package ds.tree.binaryTree
{
	import ds.tree.TreeNode;

	public class BinaryTreeNode extends TreeNode
	{
		public static const MAX_CHILD_NUM:uint = 2;
		
		public function BinaryTreeNode(parentNode:TreeNode, data:*)
		{
			this.parentNode = parentNode;
			this._childNodes = new Vector.<TreeNode>(MAX_CHILD_NUM,true);
			this._childNumLimit = MAX_CHILD_NUM;
		}
		
		public function get leftNode():BinaryTreeNode
		{
			var node:TreeNode = this._childNodes[0];
			return node ? node as BinaryTreeNode : null;
		}
		
		public function get rightNode():BinaryTreeNode
		{
			var node:TreeNode = this._childNodes[1];
			return node ? node as BinaryTreeNode : null;
		}
		
		public function set leftNode(node:BinaryTreeNode):void
		{
			this._childNodes[0] = node;
		}
		
		public function set rightNode(node:BinaryTreeNode):void
		{
			this._childNodes[1] = node;
		}
		
		override public function compare(data:*):int
		{
			if( data == this.data )
				return 0;
			
			return this.data > data ? 1:-1;
		}
		
		public function clearChildren():void
		{
			disconnect( this.leftNode );
			disconnect( this.rightNode );
		}
		
		public function transferChildrenFrom( srcNode:BinaryTreeNode ):void
		{
			if(!srcNode)
				return;
			
			var tempNodes:Vector.<TreeNode> = srcNode.childNodes.concat();
			srcNode.clearChildren();
			
			for(var i:uint = 0;i<tempNodes.length;++i)
			{
				var tempNode:BinaryTreeNode = tempNode[i];
				if( tempNode )
				{
					this._childNodes[i] = tempNode;
					tempNode.parentNode = this;
				}
			}
		}
	}
}